package edu.gduf.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import edu.gduf.dto.SigninSystemResult;
import edu.gduf.entity.Teacher;
import edu.gduf.service.TeacherService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/**
 * Created by zhang on 2017/3/24.
 */
@RestController
@RequestMapping("/signin")
public class LoginController {
    @Autowired
    TeacherService teacherService;
    @ApiOperation(value = "验证验证码", notes="验证验证码的正确性")
    @ApiImplicitParam(name = "verifycode", value = "验证码", required = true, dataType = "String")
    @RequestMapping(value = "/verify/{verifycode}",method = RequestMethod.POST)
    public SigninSystemResult kaptchaCode(@PathVariable(value ="verifycode") String kaptchaCode, HttpServletRequest request) {
        Object code = request.getSession().getAttribute("kaptcha.code");;
        if(code == null)
            return new SigninSystemResult(false,"请先获取验证码！");
        if (code.toString().equals(kaptchaCode))
            return new SigninSystemResult(true,"验证码输入正确！");
        else
            return new SigninSystemResult(false,"验证码错误，请重新输入！");
    }
    @ApiOperation(value = "教师登录",notes = "")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tId", value = "教师工号", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "password", value = "教师密码", required = true, dataType = "String")
    })
    @RequestMapping(value = "/teacher/login", method = RequestMethod.POST)
	public SigninSystemResult login(@RequestParam String tId, @RequestParam String tPassword, HttpSession session) {
		SigninSystemResult signinSystemResult;
		if (tId == null) {
			signinSystemResult = new SigninSystemResult(false, "账号或密码错误!");
			return signinSystemResult;
		}
		int parseInt = 0;
		try {
			parseInt = Integer.parseInt(tId);
		} catch (Exception exception) {
			signinSystemResult = new SigninSystemResult(false, "账号或密码错误!");
			return signinSystemResult;
		}
		Teacher teacher = teacherService.executeLogin(parseInt, tPassword);
		if (teacher == null) {
			signinSystemResult = new SigninSystemResult(false, "账号或密码错误!");
		} else {
			signinSystemResult = new SigninSystemResult(true, "登录成功!");
			session.setAttribute("teacher", teacher);

		}
		return signinSystemResult;


    }
	//
	// @RequestMapping(value = "/loginOk", method = RequestMethod.GET)
	// public void loginOk(HttpServletRequest request, HttpServletResponse
	// response) throws ServletException, IOException {
	//
	// request.getRequestDispatcher("/WEB-INF/jsp/main.jsp").forward(request,
	// response);
	// }

}
